package com.example.lzy.listviewsample.ExpandableListView;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.example.lzy.listviewsample.R;

import java.util.ArrayList;
import java.util.List;

public class ExpandableListActivity extends AppCompatActivity {

    private ExpandableListView expandableListView = null;
    private List<String> mGroupList = null;
    private List<List<String>> mItemList = null;

    private MyExpandableListViewAdapter myExpandableListViewAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandlist_layout);

        expandableListView = (ExpandableListView) findViewById(R.id.expandlist);
        expandableListView.setGroupIndicator(null);

        initData();

        myExpandableListViewAdapter = new MyExpandableListViewAdapter(this,mGroupList,mItemList);
        expandableListView.setAdapter(myExpandableListViewAdapter);

        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {

                if (mGroupList.get(i).isEmpty())
                {
                    return true;
                }

                return false;
            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {

                Toast.makeText(ExpandableListActivity.this, myExpandableListViewAdapter.getGroup(i)+";"+myExpandableListViewAdapter.getChild(i,i1), Toast.LENGTH_SHORT).show();
                return false;
            }
        });

    }

    private void initData() {
        mGroupList = new ArrayList<String>();
        mGroupList.add("123");
        mGroupList.add("13");
        mGroupList.add("r4w3");
        mGroupList.add("hrt");

        mItemList = new ArrayList<List<String>>();

        List<String> item = new ArrayList<String>();
        item.add("1");
        item.add("2");
        item.add("3");


        mItemList.add(item);

        List<String> item1 = new ArrayList<String>();
        item1.add("1");
        item1.add("2");
        item1.add("3");
        item1.add("4");

        mItemList.add(item1);
        List<String> item2 = new ArrayList<String>();
        item2.add("1");
        item2.add("2");
        item2.add("3");
        item2.add("4");

        mItemList.add(item2);
        List<String> item3 = new ArrayList<String>();
        item3.add("1");
        item3.add("2");
        item3.add("3");
        item3.add("4");
        item3.add("5");

        mItemList.add(item3);
    }
}
